﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;
using System.Data;
namespace BLL
{
    public class SecurityServices
    {
        #region Permiss - User
        public static bool HasPermission(string ModuleID, string UserName, TypeAudit audit)
        {
            DataTable dtb = new CmsBLL().GroupModule_GetListPermissionNumberByUserNameAndModuleID(ModuleID, UserName);
            if (dtb.Rows.Count > 0)
            {
                for (int i = 0; i < dtb.Rows.Count; i++)
                {
                    if (Utils.HasPermission(audit, int.Parse(dtb.Rows[i]["PermissionNumber"].ToString())))
                    {
                        return true;
                    }                    
                }
            }
            return false;
        }
        #endregion       
        #region EncryptionPassword
        public static string GetMD5(string chuoi)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);
            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);
            foreach (byte b in mang)
            {
                str_md5 += b.ToString("X2");
            }
            return str_md5;
        }
        #endregion
        #region Session And Cookie
        public static bool CheckCookieOnClient()
        {
            if (HttpContext.Current.Request.Cookies["dlploginCookie"] != null)
            {
                string UserName = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies["dlploginCookie"]["username"]).ToString();
                string Pass = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies["dlploginCookie"]["password"]).ToString();
                try
                {
                    string Password = SecurityServices.GetMD5(Pass.Trim());
                    int CheckUserLogin = int.Parse("0" + new CmsBLL().Manager_CheckLogin(UserName.Trim(), Password, DateTime.Now));
                    if (CheckUserLogin == 0)
                        return true;
                    else return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
                return false;
        }
        public static string GetUserNameFromCookie()
        {
            return HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies["dlploginCookie"]["username"]).ToString();
        }
        public static string GetCurrentUserName()
        {
            string RetUserName = string.Empty;
            if (HttpContext.Current.Session["s2username"] != null)
                RetUserName = HttpContext.Current.Session["s2username"].ToString();
            return RetUserName;
        }
        public static void SetCurrentUser(string UserName)
        {
            HttpContext.Current.Session["s2username"] = UserName;
        }
        public static string GetCurrentUser()
        {
            return HttpContext.Current.Session["s2username"].ToString();
        }
        public static void SetCookie(bool RememberAccount, string UserName, string Password)
        {
            if (RememberAccount)
            {
                HttpCookie loginCookie = new HttpCookie("dlploginCookie");
                HttpContext.Current.Response.Cookies.Remove("dlploginCookie");
                HttpContext.Current.Response.Cookies.Add(loginCookie);
                loginCookie.Values.Add("username", UserName);
                loginCookie.Values.Add("password", Password);
                DateTime dtExpiry = DateTime.Now.AddDays(15);
                HttpContext.Current.Response.Cookies["dlploginCookie"].Expires = dtExpiry;
            }
            // Set a cookie to expire after 1 second or else
            else   // if remember me is not checked
            {
                HttpCookie loginCookie = new HttpCookie("dlploginCookie");
                //Response.Cookies.Remove("dlploginCookie");
                HttpContext.Current.Response.Cookies.Add(loginCookie);
                loginCookie.Values.Add("username", UserName);
                loginCookie.Values.Add("password", Password);
                DateTime dtExpiry = DateTime.Now.AddDays(-30); //you can add years and months too here
                HttpContext.Current.Response.Cookies["dlploginCookie"].Expires = dtExpiry;
            }
        }
        #endregion        
    }

    public class MaHoaMK_BLL
    {
        public string GetMD5(string chuoi)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);
            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);
            foreach (byte b in mang)
            {
                str_md5 += b.ToString("X2");
            }
            return str_md5;
        }
    }
}
